﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"

#include "../global/head.h"

#include "../dir_monitor/head.h"
#include "../dynamic/head.h"
#include "../thread/head.h"
#include "../socket/head.h"
#include "../misc/head.h"
#include "../time/head.h"
#include "../file/head.h"
#include "../sync/head.h"
#include "../crt/head.h"
#include "../select/head.h"

#include "xos_impl.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos
{

    xos_impl::xos_impl()
    {
    }

    xos_impl::~xos_impl()
    {
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    // 
    // create xos object
    // 
    int xos_impl::create( enumXosObj enumType, void ** ppv )
    {
        int nRet = 0;

        if( ppv )
        {
            *ppv = 0;
        }

        switch( enumType )
        {
        case XOS_OBJ_DIR_MONITOR:
            {
                dir_monitor_impl * p = dir_monitor_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_CRT:
            {
                crt_impl * p = crt_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_DYNAMIC:
            {
                dynamic_impl * p = dynamic_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_DIR:
            {
                dir_impl * p = dir_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_FILE:
            {
                file_impl * p = file_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_MISC:
            {
                misc_impl * p = misc_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_SELECT:
            {
                select_impl * p = select_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_SOCKET:
            {
                socket_impl * p = socket_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_MUTX_LOCK:
            {
                mutex_impl * p = mutex_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_SPIN_LOCK:
            {
                spin_impl * p = spin_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_EVENT:
            {
                event_impl * p = event_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;
        
            break;
        case XOS_OBJ_THREAD:
            {
                thread_impl * p = thread_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_TIME:
            {
                time_impl * p = time_impl::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;

        default:
            {
                nRet = 1;
            }
            break;
        }

        return nRet;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int xos_impl::init()
    {
        int ret = 0;

        if( 0 == ret )
        {
            ret = dir_monitor_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = dynamic_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = thread_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = socket_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = select_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = misc_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = time_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = dir_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = file_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = event_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = mutex_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = spin_impl::init_all();
        }

        if( 0 == ret )
        {
            ret = crt_impl::init_all();
        }

        return ret;
    }

    int xos_impl::term()
    {
        int nRet = 0;

		// 
		// thread, should release second
		// 
        thread_impl::term_all();

        // 
        // dir
        // 
        dir_monitor_impl::term_all();

        // 
        // select
        // 
        select_impl::term_all();

        // 
        // socket
        // 
        socket_impl::term_all();

        // 
        // file path
        //
        dir_impl::term_all();
        file_impl::term_all();

        // 
        // sync
        // 
        event_impl::term_all();
        mutex_impl::term_all();
        spin_impl::term_all();

        // 
        // misc
        //
        misc_impl::term_all();

        // 
        // time
        //
        time_impl::term_all();

        // 
        // crt
        // 
        crt_impl::term_all();

        // 
        // dynamic
        // 
        dynamic_impl::term_all();

        // 
        // xos impl
        // 
        g_pXos = 0;

        return nRet;
    }

    int xos_impl::put_back_to_pool( T * pT, bool bLock )
    {
        int ret = 0;
        delete this;
        return ret;
    }

} // xos
